package com.cgc.venus.server.user.controller;

import com.cgc.venus.server.core.bean.page.PageQuery;
import com.cgc.venus.server.core.bean.response.VenusResponse;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.user.bean.RoleBean;
import com.cgc.venus.server.user.inter.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xyr on 2017/9/11.
 * 用户中心_用户管理_角色维护
 */
@Api(value = "角色维护",description="角色维护所对应服务的api")
@RequestMapping("/role-admin")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /*
    * 获得角色信息,填充下拉框
    * @return {id,name}
    * */
    @ApiOperation("得到角色信息,填充选择下拉框")
    @RequestMapping(value = "/combobox", method = RequestMethod.GET)
    public VenusResponse<Object> getRoleToSelect() throws VenusException {
        //int code = VenusResponseStatus.NOT_FOUND;
        //String message = "系统还没录入角色信息";
        // 得到角色信息
        List<Map<String,Object>> resultList = roleService.getRoleToSelect();
        /*if (resultList.size() > 0) {
            code = VenusResponseStatus.OK;
            message = "success";
        }*/
        VenusResponse<Object> response = new VenusResponse<Object>(VenusResponseStatus.OK,"",resultList);
        return response;
    }

    /*
    * 得到角色信息
    * */
    @ApiOperation(value = "角色基本信息分页查询接口", notes = "查询字段 page, pageSize")
    @RequestMapping(value = "/role", method = RequestMethod.GET)
    public VenusResponse<Object> getRole(@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("queryKey") String queryKey) throws VenusException {
        //int code = VenusResponseStatus.NOT_FOUND;
        //String message = "在系统上没找到角色信息";

        PageQuery pageQuery = new PageQuery();
        pageQuery.setQueryKey(queryKey);
        pageQuery.setPageSize(pageSize);
        pageQuery.setPage(page);

        List<Map<String,Object>> mapList = roleService.getRole(pageQuery);
        Map<String,Object> dataMap = new HashMap<String, Object>();
        long total = 0;
        if (mapList.size() > 0) {
            // code = VenusResponseStatus.OK;
            // message = "success";
            total = roleService.countRole();
        }

        dataMap.put("total",total);
        dataMap.put("items",mapList);

        VenusResponse<Object> response = new VenusResponse<Object>(VenusResponseStatus.OK,"",dataMap);
        return response;
    }

    /*
    * 新增角色信息
    * */
    @ApiOperation(value = "新增角色基本信息接口", notes = "新增角色信息:name,described,isSuperAdmin,function")
    @RequestMapping(value = "/role", method = RequestMethod.POST)
    public VenusResponse<Object> addRole(@RequestBody RoleBean roleBean) throws VenusException {
        // 非空校验
        if (StringUtils.isBlank(roleBean.getName()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"角色名为空");
        if (StringUtils.isBlank(roleBean.getDescribed()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"角色描述为空");

        int code = VenusResponseStatus.OK;
        String message = "新增角色信息,成功";

        // 判断是否存在相同名称的角色名称
        boolean exist = roleService.exist(roleBean.getName());

        if (exist) {
            // 601 数据重复
            code = VenusResponseStatus.DATA_REPEAT;
            message = "新增角色信息重复,请重新确认";
            // 返回信息
            throw new VenusException(code,message);
        } else {
            // 新增
            int row = roleService.addRole(roleBean);
        }
        VenusResponse<Object> response = new VenusResponse<Object>(code,message,"");
        return response;
    }

    /*
    * 修改角色信息
    * */
    @ApiOperation(value = "修改角色基本信息接口", notes = "修改角色信息:name,described,isSuperAdmin,function")
    @RequestMapping(value = "/role", method = RequestMethod.PATCH)
    public VenusResponse<Object> updateRole(@RequestBody RoleBean roleBean) throws VenusException {
        // 非空校验
        if (StringUtils.isBlank(roleBean.getId()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"角色ID为空");
        if (StringUtils.isBlank(roleBean.getName()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"角色名为空");
        if (StringUtils.isBlank(roleBean.getDescribed()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"角色描述为空");

        int code = VenusResponseStatus.HANDLE_ERROR;
        String message = "修改角色信息,失败";

        int row = roleService.updateRole(roleBean);

        if (row > 0) {
            code = VenusResponseStatus.OK;
            message = "修改角色信息,成功";
        } else {
            throw new VenusException(code,message);
        }
        VenusResponse<Object> response = new VenusResponse<Object>(code,message,"");
        return response;
    }

    /*
    * 根据用户ID删除用户信息
    * */
    @ApiOperation(value = "删除角色信息接口", notes = "删除角色信息")
    @RequestMapping(value = "/role/{id}", method = RequestMethod.DELETE)
    public VenusResponse<Object> deleteRole(@PathVariable("id") String id) throws VenusException {
        // 非空校验
        if (StringUtils.isBlank(id))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"角色ID为空");

        int code = VenusResponseStatus.HANDLE_ERROR;
        String message = "删除角色信息失败";

        // 1: 首先判断该是否有用户属于这个角色
        List<Map<String,Object>> userList = roleService.getUserByRoleId(id);

        if (userList.size() > 0) {
            code = VenusResponseStatus.FOREIGN_KEY_CONSTRAINTS;
            message = "此角色下面,关联用户信息,不能删除";
        } else {
            int row = roleService.deleteRole(id);
            if (row > 0) {
                code = VenusResponseStatus.OK;
                message = "成功删除角色信息" ;
            }
        }

        if (code != VenusResponseStatus.OK) {
            throw new VenusException(code,message);
        }

        VenusResponse<Object> response = new VenusResponse<Object>(code,message,"");
        return response;
    }




}
